<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name
 * @param
 *         $xxx = 作用
 * @static
 *         XXX = 作用
 *
 *
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');
include_once(SERVER_ROOT_PATH.'/include/config/ml_catelog.php');


class ml_mq_goods_analyse extends MqClass{
    const QUEUE_NAME = ML_QUEUENAME_GOODS_ANALYSE;
    private $oAnalyse , $oGuang24 , $oGuang7d , $oGuangByCtg , $oGuangByTag , $oContent, $oRedis;
    public function __construct($queue)
    {
        $this->oAnalyse = new ml_biz_analyseTags();
        $this->oContent = new ml_model_dbContent();
        $this->oGuangGdTmp = new ml_model_admin_dbGoodsAudit();
        $this->oRedis = new ml_model_rdsCount();
        

        parent::__construct($queue);
    }
    /**
     * 注释
     *
     * @param array $message_data
     * array(
     *                        所接收的参数说明
     *        )
     * )
     */
    public function run_job(){
        global $ML_CATELOG;
            
        //接收的数据
        $arr = $this->src_data;
        $rid = $arr['rid'];
        $title_ext = $arr['title_ext'];

        //取数据
        $oContent = new ml_model_dbContent();
        $rs = $oContent->get_by_rid($rid);
        /**
         * @todo
         */
        $aContent = $oContent->get_data();
        if(empty($aContent))
        {
            sleep(2);
            $rs = $oContent->get_by_rid($rid);
            /**
             * @todo
             */
            $aContent = $oContent->get_data();

            if(empty($aContent)){
                Tool_logger::debugLog(__CLASS__, $rid);
                return true;
            }
        }
        echo "------get_content\n";
        
        /*
        if(!empty($title_ext)){
            $title_array = explode('###', $title_ext);
            if(count($title_array)>1){
                $title = $title_array[0];
                $tagname_array = explode(',', $title_array[1]);
            }else{
                $title = '';
                $tagname_array = explode(',', $title_array[0]);
            }
        }else{
            $title = '';
        }
         */
        

        //分析商品标题
        $this->oAnalyse->start($aContent['gd_title'].$title_ext);
        $rs = $this->oAnalyse->segment();
        echo "-------segment\n";
        var_dump($rs);
        /**
         * @todo
         */
        if(!empty($tagname_array) && count($tagname_array)>0){
            $this->oAnalyse->add_tag($tagname_array);
        }
        $rs = $this->oAnalyse->check_tag_exists();
        /**
         * @todo
         */
        $aTag = $this->oAnalyse->get_available_tags();
        //以下数据只需要 array('tag1','tag2') 所以将其他DB结构去除。
        $aTag = Tool_array::format_2d_array($aTag , 'tag' , Tool_array::FORMAT_VALUE_ONLY );
        echo "-------avail_tags\n";
        var_dump($aTag);


        $ctg_id = $this->oAnalyse->analyse_catelog();
        echo "-------catelog\n";
        var_dump($ctg_id);
        Tool_logger::dataLog('ctg_map' , $ctg_id);

        $color_id = $this->oAnalyse->analyse_color();
        echo "-------color\n";
        var_dump($color_id);
        
        $sex = $this->oAnalyse->analyse_sex();
        echo "------sex\n";
        var_dump($sex);
        
        
        //往meila_guang_gd_tmp表中插宝贝
        $uid = ml_tool_resid::resid2uid($rid);
        $this->oRedis->getShopType($uid);//获得uid的属性（1好店，2普通店铺）暂时没有
        $user_type = $this->oRedis->get_data();
        echo "-------usertype\n";
        var_dump($user_type);


        $aContent['color'] = $color_id;
        $aContent['sex'] = $sex;
        switch ($user_type) {
            case ML_BAD_SHOP_TYPE://普通店铺不让进审核
                break;
            case ML_GOOD_SHOP_TYPE://好店
                $this->oRedis->getShopCount($uid);
                $num = $this->oRedis->get_data();
                if($num<=10){
                    $this->oRedis->incrShopCount($uid);
                    $GdTmpRs = $this->oGuangGdTmp->insert_content($aContent,$ctg_id,$aTag);//好店用户限10个
                }
                break;
            default://缺省普通用户

                $GdTmpRs = $this->oGuangGdTmp->insert_content($aContent,$ctg_id,$aTag);//普通用户随便进审核
                echo "-------insert_tmp\n";
                var_dump($GdTmpRs);
                break;                
        }
            
        //将TAG写加内容表
        $oContent->update_after_analyse_by_rid($rid , $ctg_id , $aTag);


        ml_tool_sendMq::update_gd_url($rid);
        return true;
    }

}


ml_run_queue_check(ml_mq_goods_analyse::QUEUE_NAME);

$conf_array = ml_factory::load_standard_conf('redis');
$conf_array = $conf_array['meila_queue'];
$xblog_obj = new ml_mq_goods_analyse(new RsQueue(ml_mq_goods_analyse::QUEUE_NAME , $conf_array));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();
?>